package com.coolgor.coldot.dao.wechat.qyh;

import java.util.List;

import com.coolgor.coldot.entity.wechat.qyh.QYHUser;
import org.apache.ibatis.annotations.*;


@Mapper
public interface QYHUserDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_qyh_user
			WHERE
				userid = #{userid} and corpid = #{corpid}	
		</script>		
	""")
	@Results(id = "QYHUserMap", value = {
		@Result(property = "userID", column = "user_id"),
		@Result(property = "userid", column = "userid"),
		@Result(property = "corpid", column = "corpid"),
		@Result(property = "name", column = "name"),
		@Result(property = "mobile", column = "mobile"),
		@Result(property = "department", column = "department"),
		@Result(property = "priority", column = "priority"),
		@Result(property = "position", column = "position"),
		@Result(property = "gender", column = "gender"),
		@Result(property = "email", column = "email"),
		@Result(property = "isleader", column = "isleader"),
		@Result(property = "avatar", column = "avatar"),
		@Result(property = "telephone", column = "telephone"),
		@Result(property = "enable", column = "enable"),
		@Result(property = "alias", column = "alias"),
		@Result(property = "extattr", column = "extattr"),
		@Result(property = "QRcode", column = "qr_code"),
		@Result(property = "externalProfile", column = "external_profile"),
		@Result(property = "externalPosition", column = "external_position")
	})
	QYHUser queryQYHUser(@Param("userid") String userid, @Param("corpid") String corpid);


	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_qyh_user
			<where>
				<if test="qyhUserCondition.userID != null">
					and user_id = #{qyhUserCondition.userID}
				</if>
				<if test="qyhUserCondition.userid != null">
					and userid = #{qyhUserCondition.userid}
				</if>
				<if test="qyhUserCondition.corpid != null">
					and corpid = #{qyhUserCondition.corpid}
				</if>
				<if test="qyhUserCondition.name != null">
					and name like '%${qyhUserCondition.name}%'
				</if>	
			</where>		
			LIMIT #{rowIndex},#{pageSize};	
		</script>		
	""")
	@ResultMap("QYHUserMap")
	List<QYHUser> queryQYHUserList(@Param("qyhUserCondition") QYHUser qyhUserCondition,
									@Param("rowIndex") int rowIndex,
									@Param("pageSize") int pageSize);

	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_wechat_qyh_user
			<where>
				<if test="qyhUserCondition.userID != null">
					and user_id = #{qyhUserCondition.userID}
				</if>
				<if test="qyhUserCondition.userid != null">
					and userid = #{qyhUserCondition.userid}
				</if>
				<if test="qyhUserCondition.corpid != null">
					and corpid = #{qyhUserCondition.corpid}
				</if>
				<if test="qyhUserCondition.name != null">
					and name like '%${qyhUserCondition.name}%'
				</if>
			</where>
		</script>
	""")
	int queryQYHUserCount(@Param("qyhUserCondition") QYHUser qyhUserCondition);


	@Insert("""
		<script>
		INSERT INTO tb_coldot_wechat_qyh_user
			(user_id,userid,corpid,name,mobile,department,priority,position,gender,
			email,isleader,avatar,telephone,enable,alias,extattr,
			status,qr_code,external_profile,external_position)
		VALUES
			(#{userID},#{userid},#{corpid},#{name},#{mobile},#{department},#{priority},#{position},
			#{gender},#{email},#{isleader},#{avatar},#{telephone},#{enable},#{alias},
			#{extattr},#{status},#{QRcode},#{externalProfile},#{externalPosition})	
		</script>		
	""")
	int insertQYHUser(QYHUser qyhUser);


	@Update("""
		<script>
			update tb_coldot_wechat_qyh_user
			<set>
				<if test="userID != null">user_id=#{userID},</if>
				<if test="name != null">name=#{name},</if>
				<if test="mobile != null">mobile=#{mobile},</if>
				<if test="department != null">department=#{department},</if>
				<if test="priority != null">priority=#{priority},</if>
				<if test="position != null">position=#{position},</if>
				<if test="gender != null">gender=#{gender},</if>
				<if test="email != null">email=#{email},</if>
				<if test="isleader != null">isleader=#{isleader},</if>
				<if test="avatar != null">avatar=#{avatar},</if>
				<if test="telephone != null">telephone=#{telephone},</if>
				<if test="enable != null">enable=#{enable},</if>
				<if test="alias != null">alias=#{alias},</if>
				<if test="extattr != null">extattr=#{extattr},</if>
				<if test="status != null">status=#{status},</if>
				<if test="QRcode != null">qr_code=#{QRcode},</if>
				<if test="externalProfile != null">external_profile=#{externalProfile},</if>
				<if test="externalPosition != null">external_position=#{externalPosition}</if>
			</set>
			WHERE
				userid = #{userid} and corpid = #{corpid}	
		</script>		
	""")
	int updateQYHUser(QYHUser qyhUser);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_qyh_user		
			<where>
				<if test="qyhUserCondition.userID != null">
					and user_id = #{qyhUserCondition.userID}
				</if>
				<if test="qyhUserCondition.userid != null">
					and userid = #{qyhUserCondition.userid}
				</if>
				<if test="qyhUserCondition.corpid != null">
					and corpid = #{qyhUserCondition.corpid}
				</if>
				<if test="qyhUserCondition.name != null">
					and name like '%${qyhUserCondition.name}%'
				</if>	
			</where>	
		</script>		
	""")
	int deleteQYHUser(@Param("qyhUserCondition") QYHUser qyhUserCondition);


	@Delete("""
		<script>
			DELETE FROM
				tb_coldot_wechat_qyh_user
			WHERE user_id IN
			<foreach collection="list" item="userID" open="(" separator="," close=")">
				#{userID}
			</foreach>	
		</script>		
	""")
	int batchDeleteQYHUser(List<Long> userIDList);

}
